<?php

/**
 * Controller is the customized base controller class.
 * All controller classes for this application should extend from this base class.
 */
class Controller extends CController {

    /**
     * @var string the default layout for the controller view. Defaults to '//layouts/column1',
     * meaning using a single column layout. See 'protected/views/layouts/column1.php'.
     */
    public $layout = '//layouts/column1';

    /**
     * @var array context menu items. This property will be assigned to {@link CMenu::items}.
     */
    public $menu = array();

    /**
     * @var array the breadcrumbs of the current page. The value of this property will
     * be assigned to {@link CBreadcrumbs::links}. Please refer to {@link CBreadcrumbs::links}
     * for more details on how to specify this property.
     */
    public $breadcrumbs = array();
    protected function showAjaxMessage($message,$success=false){
        echo CJSON::encode(array(
            'msg'=>$message,
            'success'=>$success
            
        ));
        Yii::app()->end();
    }
    public function beforeAction($action) {
        parent::beforeAction($action);
		 return true;
        $filterAction = array(
            'index', 'update', 'create',
        );
        if (!in_array($action->id, $filterAction)) {
            return true;
        }
        $currentCtrl = NULL;

        $currentCtrl = Yii::app()->controller->id;

        if (in_array(Yii::app()->controller->action->id, $filterAction)) {
            $currentAction = Yii::app()->controller->action->id;
            $currentParams = $_GET;
        } else {
            $currentAction = NULL;
            $currentParams = NULL;
        }
        $lastCtrl   = Yii::app()->session['last_controller'];
        $lastAction = Yii::app()->session['last_action'];
        $lastParams = Yii::app()->session['last_params'];

        if (isset(Yii::app()->session['last_controller']))
            if (isset(Yii::app()->session['last_action']))
                if (isset(Yii::app()->session['last_params'])) {
                    
                }

        if ($currentCtrl != NULL) {
            Yii::app()->session['last_controller'] = $currentCtrl;
        }

        if ($currentAction != NULL)
            Yii::app()->session['last_action'] = $currentAction;
        if ($currentParams != NULL)
            Yii::app()->session['last_params'] = $currentParams;
            if (isset(Yii::app()->user->privilege))
        $privilege = Yii::app()->user->privilege;
        else $privilege = array();
         $url = $this->id.'/'.$this->action->id;
         
         $permissions = Permissions::model()->findAll();
        $permissionsList = CHtml::listData($permissions,'id','link');
     
        if((in_array($this->id, $permissionsList)||in_array($url, $permissionsList))
                &&Yii::app()->user->usertype==Useraccess::Type_Admin){
         if(in_array($this->id, $permissionsList)){
             $permissionID = array_search($this->id, $permissionsList);
         }elseif(in_array($url, $permissionsList)){
              $permissionID = array_search($url, $permissionsList);
         }
      
         if(!in_array($permissionID, $privilege))
              throw new CHttpException(403, 'You do not have enough permission');
     
        }

       
  

        return true;
    }

    public function lock($table, $condition, $params) {
        $lock = Yii::app()->db->createCommand('select lockby,locktime from ' . $table . ' where ' . $condition)->bindValues($params)->queryRow();
        $flag=false;
        if ($lock == false || $lock['lockby'] == NULL) {
            $flag = true;
        } else if ($lock['locktime'] != NULL) {
            $diff = strtotime(time()) - strtotime($lock['locktime']);
            if ($diff > 60 * 60 * 30) {
                $flag = true;
            }
        }
        if ($flag) {
            Yii::app()->db->createCommand()->update($table, array(
                'lockby'    => Yii::app()->user->name,
                'locktime'  => date('Y-m-d H:i:s')
                    ), $condition, $params);
        }
    }

    public function unlock($table, $condition, $params) {

        $lastLock = Yii::app()->db->createCommand('select lockby from ' . $table . ' where ' . $condition)->bindValues($params)->queryScalar();

        if ($lastLock != false && $lastLock != NULL && $lastLock == Yii::app()->user->name) {

            Yii::app()->db->createCommand()->update($table, array(
                'lockby'    => "",
                'locktime'  => ''
                    ), $condition, $params);
        }
    }

}
